<template>
  <div class="period-search">
    <div class="period-search-child">
      <number-search
        v-if="updata"
        ref="elInput"
        :value="numValue"
        @input="numUpdate"
      ></number-search>
    </div>

    <div class="period-search-child">
      <el-select
        :disabled="selectDisabled"
        v-if="updata"
        :id="`form_${field.fieldName}`"
        :placeholder="$t('common.placeholder.select')"
        :clearable="clearable"
        :multiple="isMulti"
        ref="elSelect"
        filterable
        :value="value"
        @change="input"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.text"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
  </div>
</template>

<script>
import FormMixin from "@src/component/form/mixin/form";
import NumberSearch from '@src/component/form/components/FormNumber/extend/NumberSearch.vue';

export default {
  //计划任务-高级搜索-重复周期
  name: "period-extend-search",
  mixins: [FormMixin],
  props: {
    value: [String, Number],
    source: {
      type: Array,
    },
    clearable: {
      type: Boolean,
      default: true,
    },
    numValue: [String, Number]
  },
  data() {
    return {
      selectDisabled: true,
      updata: true,
      newValue: this.value,
      params: {
        period: '',
        periodUnit: ''
      },
    };
  },
  watch: {
    isMulti(v) {
      this.updata = false;
      setTimeout(() => {
        this.updata = true;
      });
    }
  },
  computed: {
    isMulti() {
      let setting = this.field.setting || {};
      return setting.isMulti;
    },
    options() {
      let setting = this.field.setting || {};
      let dataSource = setting.dataSource || [];
      
      dataSource = dataSource.map((d) => {
        if (typeof d === 'string') {
          return {
            text: d,
            value: d,
          };
        }
        return d;
      });
      return this.source || dataSource || [];
    },
  },
  methods: {
    numUpdate(newValue){
      this.selectDisabled = false;
      this.$set(this.params, 'period' ,newValue);
      this.$set(this.params, 'periodUnit' , this.$t('common.time.day'));
      this.update(this.params);
    },
    input(newValue) {
      this.$set(this.params, 'periodUnit' ,newValue);
      this.update(this.params);
    },
    update(newValue){
      let oldValue = null;
      this.$refs.elSelect.blur();
      this.$emit("update", { newValue, oldValue, field: this.field });//传参数
      this.$emit("input", newValue);
    }
  },
  components: {
    [NumberSearch.name]: NumberSearch
  }
};
</script>

<style lang="scss" scoped>
.period-search {
  display: flex;
  .form-number ::v-deep input {
    border-radius: 4px 0 0 4px;
  }
  .el-select ::v-deep input {
    border-radius: 0 4px 4px 0;
  }
}
</style>
